package com.eatrip.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.eatrip.bean.DatiLocale;
import com.eatrip.service.DatiLocaleService;

@Controller
@RequestMapping(value="/locale")
@SessionAttributes({"locale","ricette"})
public class LocaliController {

	@Autowired
	private DatiLocaleService datiLocaleService;
		
	@RequestMapping(method = RequestMethod.GET)
	public String locale(){
		return "locale";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String localePOST(){
		return "locale";
	}
	
	@RequestMapping(value="/idLocale/{idLocale}" , method = RequestMethod.GET)
	public String getLocale(@PathVariable(value = "idLocale") Integer idLocale,
							Model model){
		
		DatiLocale locale = datiLocaleService.getDatiLocale(idLocale);
		
		if (locale != null){
			String nomeLocale =  locale.getNomeLocale();
			model.addAttribute("locale", locale);
			
			return "redirect:/locale/"+nomeLocale+"";
		}
		return "../../../error";
	}
	
	@RequestMapping(value="/{nome}" , method = RequestMethod.GET)
	public String getLocale(@PathVariable(value = "nome") String nome,
							Model model){
		
		return "locale";
	}
	
}
